/**
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import rpc from '@ohos.rpc';
import LogUtils from './LogUtils';

const TAG = 'AbilityUtils msz';

export default class AbilityUtils {
  private static abilityUtilsInstance: AbilityUtils;

  public static getInstance(): AbilityUtils {
    if (!AbilityUtils.abilityUtilsInstance) {
      AbilityUtils.abilityUtilsInstance = new AbilityUtils();
    }
    return AbilityUtils.abilityUtilsInstance;
  }


  connectServiceExtensionAbility(parameters) {
    LogUtils.i(TAG, "connectServiceExtensionAbility start");
    let that = this;
    const want = {
      bundleName: 'com.ohos.systemui',
      abilityName: 'com.ohos.systemui.dialog',
      // 此处不会生效，因为这里只会创建一次，需要在onConnect里面处理
      // parameters: parameters
    }

    const connectOption = {
      onConnect(elementName, remote) {
        LogUtils.i(TAG, "onConnect Start elementName： " + JSON.stringify(elementName));
        that.startDialog(remote, parameters);
        LogUtils.i(TAG, "onConnect end： " + JSON.stringify(elementName));

      },
      onDisconnect(elementName) {
        LogUtils.i(TAG, "onDisconnect elementName: " + JSON.stringify(elementName));
      },
      onFailed(code) {
        LogUtils.i(TAG, "onFailed code: " + code);
      }
    };
    LogUtils.i(TAG, "connectServiceExtensionAbility doing");
    globalThis.context.connectServiceExtensionAbility(want, connectOption);
    LogUtils.i(TAG, "connectServiceExtensionAbility end");
  }

  startDialog(remote, parameters) {
    LogUtils.i(TAG, "startDialog start");
    const option = new rpc.MessageOption();
    const data = rpc.MessageSequence.create();
    const reply = rpc.MessageSequence.create();

    // key的个数
    data.writeInt(3)
    data.writeString('bundleName');
    // Fill in the bundleName that needs to pull up the UIability
    data.writeString('com.ohos.useriam.authwidget');
    data.writeString('abilityName');
    // Fill in the abilityName of the facility to be raised
    data.writeString('userauthuiextensionability');
    data.writeString('parameters');
    data.writeString(JSON.stringify(parameters));

    LogUtils.i(TAG, "startDialog sendMessageRequest start data: " + data.getSize());
    remote.sendMessageRequest(1, data, reply, option).then(
      result => {
        LogUtils.i(TAG, "startDialog sendMessageRequest result： " + JSON.stringify(result));
        if (result.errCode === 0) {
          // success
          LogUtils.i(TAG, "startDialog sendMessageRequest result success");
        }
        LogUtils.i(TAG, "startDialog sendMessageRequest result end");
      }
    ).finally(() => {
      LogUtils.i(TAG, "startDialog sendMessageRequest result finally");
      data.reclaim();
      reply.reclaim();
    })
    LogUtils.i(TAG, "startDialog end");
  }
}